/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.controller.administration;

import com.alxbrendmaquinarias.model.Employee;
import com.alxbrendmaquinarias.model.Shop;
import com.alxbrendmaquinarias.service.EmployeeService;
import com.alxbrendmaquinarias.service.ShopService;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Max
 */
@Controller
public class EmployeeRoleController implements Serializable{
    
    @Autowired
    EmployeeService employeeService;
    @Autowired
    ShopService shopService;
    
    private List<Employee> listEmployee;
    private Employee employee = new Employee();
    //////////////////////////////////////
    private List<Shop> listShop;
    
    public void initValuesForEmployee() {
        listEmployee = null;
        listShop = null;
        employee = new Employee();        
    }
    
    public void saveOrUpdateEmployee() throws Exception {
        if( employee.getId() == null ){
            employeeService.save(employee);
            initValuesForEmployee();
            FacesUtil.addMessage("Registrado", "Se registró un empleado", FacesMessage.SEVERITY_INFO);
        } else {
            employeeService.update(employee);
            initValuesForEmployee();
            FacesUtil.addMessage("Actualizado", "Se actualizó un empleado", FacesMessage.SEVERITY_INFO);
        }
    }

    public void removeEmployee(Employee employee) {
        try {
            employeeService.remove(employee);
            initValuesForEmployee();
            FacesUtil.addMessage("Eliminado", "Empleado Eliminado con éxito...", FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            //Logger.getLogger(EmployeeRoleController.class.getName()).log(Level.SEVERE, null, ex);
            FacesUtil.addMessage("ERROR", "El usuario tiene ventas, no puede ser eliminado", FacesMessage.SEVERITY_ERROR);
        }
    }
    public List<Employee> getListEmployee() throws Exception {
        if(listEmployee == null) {
            listEmployee = employeeService.list("Employee.findAll");
        }
        return listEmployee;
    }

    public List<Shop> getListShop() throws Exception {
        if(listShop == null){
            listShop = shopService.list("Shop.findAll");
        }
        return listShop;
    }

    public void setListShop(List<Shop> listShop) {
        this.listShop = listShop;
    }

    public void setListEmployee(List<Employee> listEmployee) {
        this.listEmployee = listEmployee;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    
    
}
